#include "mythread.h"

mythread::mythread(QObject *parent) : QThread(parent)
{

}

mythread::~mythread()
{

}

void mythread::run()
{
    QTcpSocket *fileClient=new QTcpSocket(this);
    fileClient->connectToHost(QHostAddress(m_senderIp),8810);
    if (fileClient->waitForConnected(3000)) {
        QFile file("./local_file.zip");
        qint64 totalSize = file.size();
        qint64 hasRead = 0;
        if (file.open(QIODevice::ReadOnly)) {
            QByteArray buffer;

            while (!file.atEnd()) {
                buffer = file.read(1024);
                qint64 writeRet = fileClient->write(buffer); // 写出数据返回值为本次写出的大小
                fileClient->waitForBytesWritten();

                hasRead += writeRet;    // 获取已经写出的总大小
                int per = hasRead *100 / totalSize;  // 计算百分比
                 emit valueSignal(per);
            }
            file.close();
            qDebug() << "文件发送完成";
        } else {
            qDebug() << "无法打开本地文件";
        }
    }
}

void mythread::addressValueSlot(QString value)
{
    this->m_senderIp=value;
}

